<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Model\Area;
use App\Service\AreaService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Swagger\Annotation as SA;
use Hyperf\Swagger\Request\SwaggerRequest;
use Psr\Container\ContainerInterface;

#[SA\HyperfServer('http')]
class SettingController extends Controller
{
    public $setting = [];

    public $listAreaData = [];

    #[Inject]
    protected AreaService $areaService;

    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct($this->container);

        $this->setting = [
            'area_type' => ['', '住宅', '商业住宅', '公寓'],
            'limit_year' => ['', '70年', '50年', '40年', '其它'],
            'build_type' => ['', '高层', '低层'],
        ];
        $this->listAreaData = [
            [
                'id' => 1,
                'area_name' => '小区1',
                'area_type' => '商业住宅',
                'finish_time' => '2004年',
                'limit_year' => '70年',
                'build_type' => '高层',
                'area_address' => '深圳市南山区',
                'area_builder' => '哪个开发商',
                'images' => [
                    'https://img1.baidu.com/it/u=1692828072,100355845&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img0.baidu.com/it/u=3561077264,396327064&fm=253&fmt=auto&app=138&f=JPEG?w=300&h=300',
                    'https://img0.baidu.com/it/u=3413171895,3510317216&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=501',
                    'https://img1.baidu.com/it/u=2727358114,3264975822&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img0.baidu.com/it/u=1409222400,2061229527&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img1.baidu.com/it/u=853831268,1521821500&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img0.baidu.com/it/u=1583054984,3961465160&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img2.baidu.com/it/u=2571440199,2797855069&fm=253&fmt=auto&app=138&f=JPEG?w=256&h=256',
                    'https://img1.baidu.com/it/u=1386278845,2184436492&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img0.baidu.com/it/u=811523287,3339805089&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                ],
            ],
            [
                'id' => 2,
                'area_name' => '小区2',
                'area_type' => '商业住宅',
                'finish_time' => '2004年',
                'limit_year' => '70年',
                'build_type' => '高层',
                'area_address' => '深圳市南山区深圳市南山区深圳市南山区深圳市南山区',
                'area_builder' => '哪个开发商',
                'images' => [
                    'https://img1.baidu.com/it/u=1692828072,100355845&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img0.baidu.com/it/u=3561077264,396327064&fm=253&fmt=auto&app=138&f=JPEG?w=300&h=300',
                    'https://img0.baidu.com/it/u=3413171895,3510317216&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=501',
                    'https://img1.baidu.com/it/u=2727358114,3264975822&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img0.baidu.com/it/u=1409222400,2061229527&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img1.baidu.com/it/u=853831268,1521821500&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img0.baidu.com/it/u=1583054984,3961465160&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img2.baidu.com/it/u=2571440199,2797855069&fm=253&fmt=auto&app=138&f=JPEG?w=256&h=256',
                    'https://img1.baidu.com/it/u=1386278845,2184436492&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    'https://img0.baidu.com/it/u=811523287,3339805089&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                ],
            ],
        ];
    }

    #[SA\Get(path: '/setting/area_setting', summary: '小区参数', tags: ['设置接口'])]
    #[SA\Response(response: '200', content: new SA\JsonContent(ref: null))]
    public function areaSetting()
    {
        return $this->response->success($this->setting);
    }

    #[SA\Get(path: '/setting/list_area_data', summary: '小区列表', tags: ['设置接口'])]
    #[SA\Response(response: '200', content: new SA\JsonContent(ref: null))]
    public function listAreaData()
    {
        $list = $this->areaService->getList();
        return $this->response->success($list);
    }

    #[SA\Get(path: '/setting/area_detail', summary: '小区详情', tags: ['设置接口'])]
    #[SA\RequestBody(content: new SA\JsonContent(properties: [
        new SA\Property(property: 'id', description: 'ID', type: 'int', rules: 'required|int'),
    ]))]
    #[SA\Response(response: '200', content: new SA\JsonContent(ref: null))]
    public function areaDetail(SwaggerRequest $request)
    {
        $get = $request->all();
        $id = (int) $get['id'];
        $detail = $this->areaService->getDetail($id);
        return $this->response->success($detail);
    }

    #[SA\Post(path: '/setting/area_add', summary: '添加小区', tags: ['设置接口'])]
    /*#[SA\RequestBody(content: new SA\JsonContent(properties: [
        new SA\Property(property: 'id', description: 'ID', type: 'int', rules: 'required|int'),
        new SA\Property(property: 'kind', description: '类型', type: 'string', rules: 'required|string'),
    ]))]*/
    #[SA\Response(response: '200', content: new SA\JsonContent(ref: null))]
    public function areaAdd(SwaggerRequest $request)
    {
        $data = $request->post();
        var_dump($data);
        if ($data) {
            $area = new Area();
            $area->area_name = $data['areaName'];
            $area->area_type = $data['areaType'];
            $area->finish_time = $data['finishTime'];
            $area->limit_year = $data['limitYear'];
            $area->build_type = $data['buildType'];
            $area->area_builder = $data['areaBuilder'];
            $area->area_address = $data['areaAddress'];
            $area->area_images = $data['areaImages'];

            $return = $this->areaService->insert($area);
            return $this->response->success($return);
        }
        return $this->response->fail(-1, '添加失败，数据不能为空');
    }

    #[SA\Put(path: '/setting/area_update', summary: '修改小区', tags: ['设置接口'])]
    /*#[SA\RequestBody(content: new SA\JsonContent(properties: [
        new SA\Property(property: 'id', description: 'ID', type: 'int', rules: 'required|int'),
        new SA\Property(property: 'kind', description: '类型', type: 'string', rules: 'required|string'),
    ]))]*/
    #[SA\Response(response: '200', content: new SA\JsonContent(ref: null))]
    public function areaUpdate(SwaggerRequest $request)
    {
        $data = $request->post();
        if ($data) {
            $area = new Area();
            $area->id = $data['id'];
            $area->area_name = $data['areaName'];
            $area->area_type = $data['areaType'];
            $area->finish_time = $data['finishTime'];
            $area->limit_year = $data['limitYear'];
            $area->build_type = $data['buildType'];
            $area->area_builder = $data['areaBuilder'];
            $area->area_address = $data['areaAddress'];
            $area->area_images = $data['areaImages'];

            $return = $this->areaService->update($area);
            return $this->response->success($return);
        }
        return $this->response->fail(-1, '修改失败，数据不能为空');
    }
}
